package no.progark.views;

import no.progark.copter.Direction;
import no.progark.pong.UpBall;
import no.progark.pong.DownBall;
import no.progark.pong.AIController;
import no.progark.pong.Ball;
import no.progark.pong.Controller;
import no.progark.pong.Scoreboard;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.view.MotionEvent;
import android.view.View;

public class PongView extends View {
	
	Ball ball;
	Controller bottomController;
	AIController topController;
	Scoreboard scores = Scoreboard.getInstance();
	
	
	int fingerPosX = 0;
	int fingerPosY = 0;
	
	boolean firstTimeDrawBall = true;
	private int topControllerSpeed = 5;
	boolean won;
	String winner;
	
	private static PongView instance = null; 
	
	protected PongView(Context context) {
		super(context);
		ball = Ball.getinstance(context);
		won = false;
		winner = "";
		bottomController = Controller.getInstance(context);
		topController = AIController.getIns(context);
		
	}
	
	public static PongView getInstace(Context context){
		if(instance == null)instance = new PongView(context);
		return instance;
	}
	
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		if (firstTimeDrawBall) {
			System.out.println("First time");
			bottomController.initPos(this.getMeasuredWidth()/2, this.getMeasuredHeight()-bottomController.CONTROLLERHEIGHT);
			topController.initPos(this.getMeasuredWidth()/2, 0);
			ball.initPos(getContext(), canvas, this.getMeasuredHeight(), this.getMeasuredWidth(), Direction.DOWN);
			firstTimeDrawBall = false;
		}
		
		
		
		
		Rect bcg = new Rect();
		bcg.set(0, 0, canvas.getWidth(), canvas.getHeight());
		
		Paint paint = new Paint();
		paint.setColor(Color.BLACK);
		paint.setStyle(Paint.Style.FILL);
		
		canvas.drawRect(bcg, paint);
		
		if(!won)canvas.drawBitmap(ball.getImage(), ball.getPosX(), ball.getPosY(), paint);
		
		if(ball.update(canvas, bottomController, topController)){
			if(ball.getPosY() < 0){
				won = scores.incrementScoreAndCheckWin("p1");
				System.out.println("won is " + won);
				if(won){
					winner = "YOU";
					ball.setVelocity(0);
				}
				ball.initPos(getContext(), canvas, this.getMeasuredHeight(), this.getMeasuredWidth(), Direction.UP);
			}
			else if(ball.getPosY() > this.getMeasuredHeight()){
				won = scores.incrementScoreAndCheckWin("p2");
				System.out.println("won is " + won);
				if(won){
					winner = "COM";
					ball.setVelocity(0);
				}
				ball.initPos(getContext(), canvas, this.getMeasuredHeight(), this.getMeasuredWidth(), Direction.DOWN);
			}
		}
		
		
		//Text
		Paint textPaint = new Paint();
		textPaint.setColor(Color.WHITE);
		textPaint.setStyle(Paint.Style.FILL);
		textPaint.setTextSize(15);
		if(!won){
			canvas.drawText(""+scores.getP1() + " : " + scores.getP2(), (this.getMeasuredWidth()/2-10), (this.getMeasuredHeight()/2+10), textPaint);
		
			topController.update(ball.getPosX()+(ball.BALLSIZE/2), topControllerSpeed );
			canvas.drawBitmap(bottomController.getImage(), bottomController.getPosX(), bottomController.getPosY(), paint);
			canvas.drawBitmap(topController.getImage(), topController.getPosX(), topController.getPosY(), paint);
		}
		else {
			textPaint.setTextSize(26);
			canvas.drawText(winner + "" + " WON! ", ((this.getMeasuredWidth()/2)-60), this.getMeasuredHeight()/2, textPaint);
			
		}
		
		invalidate();
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		bottomController.update((int) event.getX());
		fingerPosX = (int)event.getX();
		fingerPosY = (int)event.getY();
		return super.onTouchEvent(event);
	}
	
	
}